﻿@{
    Layout = "~/Views/Shared/_FormWhite.cshtml";
}
@section header {
    <link href="~/lib/summernote/0.8.15/summernote.min.css" rel="stylesheet" type="text/css">
    <script src='~/lib/summernote/0.8.15/summernote.js' type="text/javascript"></script>
    <script src='~/lib/summernote/0.8.15/lang/summernote-zh-CN.min.js' type="text/javascript"></script>

    <link href="~/lib/imageupload/1.0/css/imgup.min.css" rel="stylesheet" type="text/css">
    <script src='~/lib/imageupload/1.0/js/imgup.min.js' type="text/javascript"></script>

    <link href="~/lib/bootstrap.tagsinput/0.8.0/bootstrap-tagsinput.min.css" rel="stylesheet" type="text/css">
    <script src='~/lib/bootstrap.tagsinput/0.8.0/bootstrap-tagsinput.min.js' type="text/javascript"></script>
}

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>工序管理</title>
    <style>
        /* 颜色变量 */
        :root {
            --primary: #165DFF;
            --success: #00B42A;
            --danger: #F53F3F;
            --gray-100: #F2F3F5;
            --gray-200: #E5E6EB;
            --gray-300: #C9CDD4;
            --gray-400: #86909C;
            --gray-500: #4E5969;
            --gray-600: #272E3B;
            --gray-700: #1D2129;
        }

        /* 全局样式 */
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Inter', system-ui, sans-serif;
        }

        /* 表单容器 */
        .form-container {
            width: 100%;
            max-width: 400px;
            margin: 0 auto;
        }

        /* 表单样式 */
        .form-group {
            margin-bottom: 20px;
        }

        .form-label {
            display: block;
            color: var(--gray-700);
            font-weight: 500;
            margin-bottom: 8px;
        }

            .form-label.required::after {
                content: '*';
                color: var(--danger);
                margin-left: 4px;
            }

        .form-control-wrapper {
            position: relative;
        }

        .form-control {
            width: 100%;
            padding: 8px 12px 8px 36px;
            border: 1px solid var(--gray-300);
            border-radius: 6px;
            transition: all 0.3s ease;
        }

            .form-control:focus {
                border-color: var(--primary);
                box-shadow: 0 0 0 2px rgba(22, 93, 255, 0.2);
                outline: none;
            }

            .form-control.error {
                border-color: var(--danger);
            }

        .form-icon {
            position: absolute;
            top: 50%;
            left: 12px;
            transform: translateY(-50%);
            color: var(--gray-400);
        }

        .form-error {
            margin-top: 4px;
            color: var(--danger);
            font-size: 12px;
            display: none;
        }

            .form-error.show {
                display: block;
            }

        /* 下拉选择框样式 */
        .select-wrapper {
            position: relative;
        }

            .select-wrapper select {
                appearance: none;
                padding-right: 36px;
            }

        .select-arrow {
            position: absolute;
            top: 50%;
            right: 12px;
            transform: translateY(-50%);
            color: var(--gray-400);
            pointer-events: none;
        }

        /* 按钮样式 */
        .btn-group {
            display: flex;
            justify-content: flex-end;
            gap: 12px;
            padding-top: 16px;
        }

        .btn {
            padding: 8px 20px;
            border-radius: 6px;
            font-weight: 500;
            transition: all 0.3s ease;
            cursor: pointer;
            display: flex;
            align-items: center;
        }

            .btn span {
                margin-right: 8px;
            }

        .btn-cancel {
            border: 1px solid var(--gray-300);
            color: var(--gray-700);
            background-color: white;
        }

            .btn-cancel:hover {
                background-color: var(--gray-100);
            }

        .btn-save {
            background-color: var(--primary);
            color: white;
            border: none;
        }

            .btn-save:hover {
                box-shadow: 0 4px 12px rgba(22, 93, 255, 0.3);
                transform: translateY(-2px);
            }

        /* 提示消息样式 */
        .toast {
            position: fixed;
            top: 16px;
            right: 16px;
            background-color: var(--success);
            color: white;
            padding: 12px 24px;
            border-radius: 6px;
            box-shadow: 0 4px 12px rgba(0, 180, 42, 0.3);
            display: flex;
            align-items: center;
            z-index: 50;
            transform: translateX(120%);
            transition: transform 0.3s ease;
        }

            .toast.show {
                transform: translateX(0);
            }

            .toast span {
                margin-right: 8px;
            }
    </style>
</head>
<body class="bg-gray-100 min-h-screen flex items-center justify-center p-4">
    <div class="form-container">
        <form id="processForm">
            <!-- 工序名称 -->
            <div class="form-group">
                <label for="processName" class="form-label required">工序名称</label>
                <div class="form-control-wrapper">
                    <span class="form-icon">🏷️</span>
                    <input type="text" id="processName" name="Name" class="form-control" placeholder="请输入工序名称" required>
                </div>
                <p id="nameError" class="form-error">工序名称不能为空</p>
            </div>

            <!-- 上级工序 -->
            <div class="form-group">
                <label for="parentProcess" class="form-label">上级工序</label>
                <div class="select-wrapper form-control-wrapper">
                    <div id="parentProcess" name="ParentId" />
                </div>
            </div>

            <!-- 部门备注 -->
            <div class="form-group">
                <label for="departmentRemark" class="form-label">部门备注</label>
                <div class="form-control-wrapper">
                    <span class="form-icon">💬</span>
                    <textarea id="departmentRemark" name="DepartmentRemark" rows="3" class="form-control" placeholder="请输入部门备注信息"></textarea>
                </div>
            </div>

            <!-- 按钮组 -->
            <div class="btn-group">
                <button type="button" id="cancelBtn" class="btn btn-cancel">取消</button>
                <button type="submit" id="saveBtn" class="btn btn-save">
                    <span>💾</span>保存
                </button>
            </div>
        </form>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            // 获取表单元素
            const form = document.getElementById('processForm');
            const processName = document.getElementById('processName');
            const nameError = document.getElementById('nameError');
            const cancelBtn = document.getElementById('cancelBtn');

            // 初始化上级工序下拉框
            $(function() {
                ys.ajax({
                    url: '@Url.Content("~/PMCManage/Process/GetListJson")',
                    type: "post",
                    headers: {'Content-Type': 'application/x-www-form-urlencoded'},
                    data: { IsTop: true }, // 直接传递对象
                    success: function (obj) {
                        if (obj.Tag == 1) {
                            $('#parentProcess').ysComboBox({
                                data: obj.Data,
                                key: "Id",
                                value: "Name",
                                class:"no-all-class"//去除“全部”选项
                            });
                        } else {
                            ys.msgError(obj.Message);
                        }
                    }
                });
            });

            // 表单验证
            function validateForm() {
                let isValid = true;

                // 验证工序名称
                if (!processName.value.trim()) {
                    nameError.classList.add('show');
                    processName.classList.add('error');
                    isValid = false;
                } else {
                    nameError.classList.remove('show');
                    processName.classList.remove('error');
                }

                return isValid;
            }

            // 表单提交处理
            form.addEventListener('submit', function(e) {
                e.preventDefault();

                if (validateForm()) {
                    const name = processName.value.trim();
                    const parentId = $('#parentProcess').ysComboBox('getValue'); // 使用ysComboBox的getValue方法
                    const departmentRemark = document.getElementById('departmentRemark').value;

                    // 实际项目中这里应该发送AJAX请求到后端保存数据
                    ys.ajax({
                        url: '@Url.Content("~/PMCManage/Process/Add")',
                        type: "post",
                        data: {
                            Name: name,
                            ParentId: parentId || null,
                            DepartmentRemark: departmentRemark || ''
                        },
                        success: function (obj) {
                            if (obj.Tag == 1) {
                                // 显示成功提示
                                ys.msgSuccess(obj.Message);

                                // 重置表单
                                form.reset();
                                $('#parentProcess').ysComboBox('setValue', ''); // 重置下拉框

                                // 关闭窗口
                                ys.closeDialog();
                            } else {
                                ys.msgError(obj.Message);
                            }
                        }
                    });
                }
            });

            // 实时验证工序名称
            processName.addEventListener('input', function() {
                if (this.value.trim()) {
                    nameError.classList.remove('show');
                    this.classList.remove('border-danger');
                }
            });

            // 取消按钮
            cancelBtn.addEventListener('click', function() {
                // 确认取消
                if (confirm('确定要取消操作吗？未保存的数据将会丢失。')) {
                    form.reset();
                    nameError.classList.remove('show');
                    processName.classList.remove('error');
                }
            });

            // 触发自定义事件，通知外部弹出框表单已加载完成
            const loadedEvent = new CustomEvent('processFormLoaded', {
                bubbles: true,
                cancelable: true
            });
            document.dispatchEvent(loadedEvent);
        });
    </script>
</body>
</html>
